package leetcode.秋招.广;


import java.util.Scanner;

/**
 * @author ：zsy
 * @date ：Created 2021/9/17 22:00
 * @description：
 */
public class Main7 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int k = in.nextInt();
        in.nextLine();
        String next = in.nextLine();
        String[] s = next.split(" ");
        ListNode curHead = new ListNode(-1);
        ListNode temp = curHead;
        for (int i = 0; i < s.length; i++) {
            temp.next = new ListNode(Integer.parseInt(s[i]));
            temp = temp.next;
        }
        ListNode head = reverseLGroup(curHead.next, k);
        while (head != null) {
            System.out.print(head.val + " ");
            head = head.next;
        }
    }
    public static ListNode reverse(ListNode a, ListNode b) {
        ListNode pre = null, cur = a;
        while (cur != b) {
            ListNode temp = cur.next;
            cur.next = pre;
            pre = cur;
            cur = temp;
        }
        return pre;
    }

    public static ListNode reverseLGroup(ListNode root, int k) {
        if (root == null) return null;
        ListNode a = root, b = root;
        for (int i = 0; i < k; i++) {
            if (b == null) break;
            b = b.next;
        }
        ListNode newHead = reverse(a, b);
        a.next = reverseLGroup(b, k);
        return newHead;
    }
    static class ListNode {
        public int val;
        public ListNode next;

        public ListNode() {
        }

        public ListNode(int x) { val = x; }
        public ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
        @Override
        public String toString() {
            return "ListNode{" +
                    "val=" + val +
                    '}';
        }
    }
}
